﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Estimate.Common.DataFileValidator
{
    public class SiteForeValidator : IDataFileValidator
    {

        public ValidateResult ValidateFile(string filepath)
        {
            ValidateResult result = new ValidateResult();
            //根据文件名获取clock和span参数
            string filename = Path.GetFileName(filepath);
            int lineNumber = 0;
            int[] floatIndexes = new int[] { 4, 5, 6, 7, 8, 9, 10, 11 };

            StreamReader file = new StreamReader(filepath, Encoding.Default);

            string line = file.ReadLine();

            line = file.ReadLine();

            string pattern = @"\s*[\t|\ ]+\s*";

            lineNumber = 2;
            while (!file.EndOfStream)
            {
                line = file.ReadLine();

                string[] foreserial = Regex.Split(line, pattern, RegexOptions.IgnoreCase);

                if (foreserial[0].Length != 5)
                {
                    throw new Exception("文件格式非法，第一行必须为站号！");
                }


                int curCol = 0;

                try
                {
                    for (int i = 0; i < floatIndexes.Length; i++)
                    {
                        curCol = floatIndexes[i];
                        float floatResult = Convert.ToSingle(foreserial[curCol].ToString());
                    }
                }
                catch (FormatException e)
                {
                    string msg = string.Format("第{0}行，第{1}列数据不能转化为float数值类型。", lineNumber, curCol);
                    ValidateResultRecord record = new ValidateResultRecord(lineNumber, curCol, msg);
                    result.ErrorList.Add(record);
                }
                catch (Exception e)
                {
                    string msg = e.Message;
                    ValidateResultRecord record = new ValidateResultRecord(lineNumber, curCol, msg);
                    result.ErrorList.Add(record);
                }


                lineNumber++;
            }
            return result;
        }
    }
}
